package control;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletSession;

import core.corewa.Event;
import db.dao.DictionariesDAO;
import db.dao.LanguagesDAO;
import db.dao.ManagerInDictDAO;
import db.dao.UserDAO;
import db.dbo.ListDictionariesDBO;
import db.dto.DictionariesDTO;
import db.dto.LanguagesDTO;
import db.dto.ManagerInDictDTO;
import db.dto.UserDTO;
import form.ApproverHomeForm;
import form.BaseForm;
import form.ReviewerDetailForm;

@SuppressWarnings("serial")
public class ReviewerDetailController extends BaseController {
	/**
	 * do nothing in here.
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procHome(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		return null;
	}
	/**
	 * Logout action.
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procLogout(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		request.getPortletSession().removeAttribute("username", PortletSession.APPLICATION_SCOPE);
		request.getPortletSession().removeAttribute("role", PortletSession.APPLICATION_SCOPE);
		request.getPortletSession().setAttribute("username", "Guest",PortletSession.APPLICATION_SCOPE);
    	request.getPortletSession().setAttribute("role", -1, PortletSession.APPLICATION_SCOPE);
		return null;
	}
	public BaseForm procDelete(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		int reviewerId = Integer.parseInt(request.getParameter("reviewerId"));
		int dictId = Integer.parseInt(request.getParameter("dictId"));
		ManagerInDictDAO managerDictDAO = new ManagerInDictDAO();
		managerDictDAO.deleteManagerInDict(dictId, reviewerId);
		DictionariesDAO dictDAO = new DictionariesDAO();
		DictionariesDTO dictDTO = dictDAO.getDictionary(dictId);
		ReviewerDetailForm result = new ReviewerDetailForm();
		LanguagesDAO lanDAO = new LanguagesDAO();
		LanguagesDTO from = lanDAO.getLanguage(dictDTO.getFrom());
		LanguagesDTO to = lanDAO.getLanguage(dictDTO.getTo());
		result.setDictName(from.getName()+" - "+to.getName());
		result.setDictId(dictId);
		UserDAO userDAO = new UserDAO();
		List<ManagerInDictDTO> listManagerDTO = managerDictDAO.getManagerInDictDict(dictId);
		List<UserDTO> listReviewer = new ArrayList<UserDTO>();  
		for (ManagerInDictDTO managerInDictDTO : listManagerDTO) {
			UserDTO userDTO = userDAO.getUser(managerInDictDTO.getUserID());
			if(userDTO.getRole() == 1) {
				listReviewer.add(userDTO);
			}
		}
		result.setListReviewer(listReviewer);
		return result;
	}
	public BaseForm procDeleteList(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		String[] reviewerId = request.getParameterValues("userId");
		int dictId = Integer.parseInt(request.getParameter("dictId"));
		ManagerInDictDAO managerDictDAO = new ManagerInDictDAO();
		for(int i = 0; i < reviewerId.length; i++) {
			managerDictDAO.deleteManagerInDict(dictId, Integer.parseInt(reviewerId[i]));
		}
		DictionariesDAO dictDAO = new DictionariesDAO();
		DictionariesDTO dictDTO = dictDAO.getDictionary(dictId);
		ReviewerDetailForm result = new ReviewerDetailForm();
		LanguagesDAO lanDAO = new LanguagesDAO();
		LanguagesDTO from = lanDAO.getLanguage(dictDTO.getFrom());
		LanguagesDTO to = lanDAO.getLanguage(dictDTO.getTo());
		result.setDictName(from.getName()+" - "+to.getName());
		result.setDictId(dictId);
		UserDAO userDAO = new UserDAO();
		List<ManagerInDictDTO> listManagerDTO = managerDictDAO.getManagerInDictDict(dictId);
		List<UserDTO> listReviewer = new ArrayList<UserDTO>();  
		for (ManagerInDictDTO managerInDictDTO : listManagerDTO) {
			UserDTO userDTO = userDAO.getUser(managerInDictDTO.getUserID());
			if(userDTO.getRole() == 1) {
				listReviewer.add(userDTO);
			}
		}
		result.setListReviewer(listReviewer);
		return result;
	}
	public BaseForm procBack(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		String username = (String) request.getPortletSession().getAttribute("username", PortletSession.APPLICATION_SCOPE);
		ApproverHomeForm approverForm = new ApproverHomeForm();
		ListDictionariesDBO listDict = new ListDictionariesDBO();
    	approverForm.setListDictionaries(listDict.getListDictionariesApprover(username));
    	return approverForm;
	}
}
